import { createRouter, createWebHashHistory } from 'vue-router'

const routes = [
	{
		path: '/',
		name: 'Home',
		component: () => import(/* webpackChunkName: 'home' */ '../views/home/Home')
	},
	{
		path: '/shop/:id',
		name: 'Shop',
		component: () => import(/* webpackChunkName: 'shop' */ '../views/shop/Shop')
	},
	{
		path: '/cart',
		name: 'Cart',
		component: () => import(/* webpackChunkName: 'cart' */ '../views/cart/Cart')
	},
	{
		path: '/order/:id',
		name: 'Order',
		component: () => import(/* webpackChunkName: 'order' */ '../views/order/Order')
	},
	{
		path: '/orderList',
		name: 'OrderList',
		component: () => import(/* webpackChunkName: 'orderList' */ '../views/orderList/orderList')
	},
	{
		path: '/me',
		name: 'Me',
		component: () => import(/* webpackChunkName: 'me' */ '../views/me/Me')
	},
	{
		path: '/login',
		name: 'Login',
		component: () => import(/* webpackChunkName: 'login' */ '../views/login/Login'),
		beforeEnter(to, from, next) {
			const { isLogin } = localStorage
			isLogin ? next({ name: 'Home' }) : next()
		}
	}
]

const router = createRouter({
  	history: createWebHashHistory(),
  	routes
})

router.beforeEach((to, from, next) => {
	const { isLogin } = localStorage
	const isLoginOrRegister = (to.name === 'Login' || to.name === 'Register')
	isLogin || isLoginOrRegister ? next() : next({ name: 'Login' })
})

export default router
